using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace HelloXNA2.Component._3D._3DModel1
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Demo : Microsoft.Xna.Framework.DrawableGameComponent
    {
        private MyCamera _camera;
        private List<MyModel> _models = new List<MyModel>();

        public Demo(Game game)
            : base(game)
        {
            // TODO: Construct any child components here
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here
            _camera = new MyCamera(Game, new Vector3(0, 0, 50), Vector3.Zero, Vector3.Up);

            Game.Components.Add(_camera);


            base.Initialize();
        }

        protected override void LoadContent()
        {
            _models.Add(new MyModel(Game.Content.Load<Model>(@"3DModel\Spaceship"), Matrix.Identity * Matrix.CreateTranslation(-10, -5, -20)));
            _models.Add(new MyModel(Game.Content.Load<Model>(@"3DModel\Spaceship"), Matrix.Identity * Matrix.CreateTranslation(10, 5, 0))); 
 

            base.LoadContent();
        }

        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here
            KeyboardState keyboardState = Keyboard.GetState();

            foreach (MyModel model in _models)
            {
                if (keyboardState.IsKeyDown(Keys.Up))
                    model.Rotate(RotationDirection.Up);
                else if (keyboardState.IsKeyDown(Keys.Down))
                    model.Rotate(RotationDirection.Down);
                else if (keyboardState.IsKeyDown(Keys.Left))
                    model.Rotate(RotationDirection.Left);
                else if (keyboardState.IsKeyDown(Keys.Right))
                    model.Rotate(RotationDirection.Right);
            } 
  


            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            foreach (var model in _models)
            {
                model.Draw(_camera);
            }

            base.Draw(gameTime);
        }
    }
}
